﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EWPlatAPP.UControls
{
    public partial class FormParent : Form
    {
        public FormParent()
        {
            InitializeComponent();
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);//忽略窗口消息，减少闪烁
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//绘制到缓冲区，减少闪烁
            SetStyle(ControlStyles.UserPaint, true);//控件由其自身而不是操作系统绘制
            SetStyle(ControlStyles.ResizeRedraw, true);//控件调整其大小时重绘
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景
            
            r = this.ClientRectangle;
            ResetTitlePanel();
        }

        public new FormBorderStyle FormBorderStyle
        {
            get
            {
                return base.FormBorderStyle;
            }
            set
            {
                if (value != FormBorderStyle.Sizable && value != FormBorderStyle.SizableToolWindow)
                {
                    pTop.Controls.Remove(btnMax);
                   
                }
                base.FormBorderStyle = value;
            }
        }

        public new bool TopLevel
        {
            get
            {
                return base.TopLevel;
            }
           set {
                base.TopLevel = value;
                if (value==false)
                {
                    pTop.Visible = false;
                    BorderWidth = 0;
                }
              
            }
        }

        public new bool ShowIcon
        {
            get
            {
                return base.ShowIcon;
            }
            set
            {
                base.ShowIcon = value;
                if(value==false)
                {
                    pbIco.Visible = false;
                    lblFormTitle.Left = 12;
                }
            }
        }




        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("窗体图标")]
        public string FrmIcon
        {
            get { return this.pbIco.ImageLocation; }
            set
            {
                pbIco.ImageLocation = value;
            }
        }

        public new string Text
        {
            get { return this.lblFormTitle.Text; }
            set
            {
                base.Text = value ;
            }
        }



        [Browsable(false)]
        public new bool ControlBox
        {
            get
            {
                return false;
            }
            set
            {
                base.ControlBox = false;
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("窗体标题")]
        public string Title
        {
            get { return lblFormTitle.Text; }
            set { lblFormTitle.Text = value;
                this.Text = value;
            }
        }



        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("窗体标题字体样式")]
        public Font TitleFont
        {
            get
            {
                return lblFormTitle.Font;
            }
            set
            {
                lblFormTitle.Font = value;
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("窗体标题字体颜色")]
        public Color TitleColor
        {
            get
            {
                return lblFormTitle.ForeColor;
            }
            set
            {
                lblFormTitle.ForeColor = value;
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("窗体边框颜色")]
        private Color borderColor = Color.Blue;
        public Color BorderColor
        {
            get
            {
                return borderColor;
            }
            set
            {
                borderColor = value;
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Description("窗体标题栏背景色")]
        public Color TitleBarBackColor
        {
            get
            {
                return pTop.BackColor;
            }
            set
            {
                pTop.BackColor = value;
                Invalidate();
            }
        }



        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DefaultValue(typeof(int), "2"), Description("圆角弧度大小")]
        private int radius = 2;
        public int Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                Invalidate();
            }
        }

        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DefaultValue(typeof(int), "1"), Description("边框粗细")]
        private int borderWidth = 1;
        public int BorderWidth
        {
            get { return borderWidth; }
            set
            {
                borderWidth = value;
                Invalidate();
            }
        }

        public new FormWindowState WindowState
        {
            get
            {
                return base.WindowState;
            }
            set
            {
                base.WindowState = value;
                if(value==FormWindowState.Maximized)
                {
                    btnMax.Text = "2";
                }
            }
        }


        public new bool MaximizeBox
        {
            get
            {
                return pTop.Contains(btnMax);
            }
            set
            {
                if (!value)
                {
                    pTop.Controls.Remove(btnMax);
                }
                else if (!pTop.Contains(btnMax))
                {
                    if (pTop.Contains(btnMin))
                    {
                        btnMin.Left = btnClose.Left+140;
                    }
                    else
                        btnMax.Left = btnClose.Left;
                    pTop.Controls.Add(btnMax);
                }
            }
        }

        public new bool MinimizeBox
        {
            get
            {
                return pTop.Contains(btnMin);
            }
            set
            {
                if (!value)
                {
                    pTop.Controls.Remove(btnMin);
                }
                else if (!pTop.Contains(btnMin))
                {
                    if (pTop.Contains(btnMax))
                    {
                        btnMin.Left = btnClose.Left +40;
                    }
                    else
                        btnMin.Left = btnClose.Left +40;
                    pTop.Controls.Add(btnMin);
                }
            }
        }

        private void ResetTitlePanel()
        {
            base.ControlBox = false;
            base.Text = null;
            SetToolTip(btnClose, "关闭");
            // btnMax.Size = btnClose.Size;
            SetToolTip(btnMax, "最大化或还原");
            //   btnMin.Size = btnClose.Size;
            SetToolTip(btnMin, "最小化");
           
       
        }

        private void SetToolTip(Control ctrl, string tip)
        {
            new ToolTip().SetToolTip(ctrl, tip);
        }

        private void FormParent_Load(object sender, EventArgs e)
        {

        }

        Point point = new Point();
        bool isMove = false;

        private void pTop_MouseDown(object sender, MouseEventArgs e)
        {
            point = e.Location;//按住的点
            isMove = true;
        }

        private void pTop_MouseMove(object sender, MouseEventArgs e)
        {
            Point pointNew;
            if (e.Button == MouseButtons.Left && isMove)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                    btnMax.Text = "1";
                    point = new Point(pTop.Left + pTop.Width / 2, pTop.Top + 10);
                }


                pointNew = e.Location;//按住的点拖动到的位置
                Point fPointNew = new Point(pointNew.X - point.X, pointNew.Y - point.Y);//相对于原来起点的距离点的描述
                this.Location += new Size(fPointNew);
            }
        }

        const int WM_NCHITTEST = 0x0084;// 移动鼠标
        const int HTLEFT = 10;
        const int HTRIGHT = 11;
        const int HTTOP = 12;
        const int HTTOPLEFT = 13;
        const int HTTOPRIGHT = 14;
        const int HTBOTTOM = 15;
        const int HTBOTTOMLEFT = 0x10;
        const int HTBOTTOMRIGHT = 17;
        const int WM_PAINT = 0xF;
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            switch (m.Msg)
            {
                //case WM_PAINT:
                //    //背景区域的矩形
                //    PaintBackground();
                //    break;
                case WM_NCHITTEST:
                    if (this.MaximizeBox == true && this.WindowState == FormWindowState.Normal)
                    {
                        Point vPoint = new Point((int)m.LParam & 0xFFFF,
              (int)m.LParam >> 16 & 0xFFFF);
                        vPoint = PointToClient(vPoint);
                        if (vPoint.X <= 5)
                            if (vPoint.Y <= 5)
                                m.Result = (IntPtr)HTTOPLEFT;
                            else if (vPoint.Y >= ClientSize.Height - 5)
                                m.Result = (IntPtr)HTBOTTOMLEFT;
                            else m.Result = (IntPtr)HTLEFT;
                        else if (vPoint.X >= ClientSize.Width - 5)
                            if (vPoint.Y <= 5)
                                m.Result = (IntPtr)HTTOPRIGHT;
                            else if (vPoint.Y >= ClientSize.Height - 5)
                                m.Result = (IntPtr)HTBOTTOMRIGHT;
                            else m.Result = (IntPtr)HTRIGHT;
                        else if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOP;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOM;
                    }


                    break;
            }
        }
        Rectangle r;
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            r = this.ClientRectangle;
            if (radius > 0)
                this.Region = new Region(PaintCommon.GetRoundRectangle(r, Radius));
            else
                this.Region = new Region(r);
            r.Width -= 1;
            r.Height -= 1;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Brush b = new SolidBrush(BackColor);
            GraphicsPath path = new GraphicsPath();
            if (this.Radius > 0)
            {
                path = PaintCommon.GetRoundRectangle(r, Radius);//绘制圆角矩形路径
                g.FillPath(b, path);//填充圆角矩形内部
                if (BorderWidth > 0)
                {
                    using (Pen pen = new Pen(BorderColor, BorderWidth))
                    {
                        g.DrawPath(pen, path);
                    }
                }
            }
            else
            {
                g.FillRectangle(b, r);//填充圆角矩形内部
                if (BorderWidth > 0)
                {
                    using (Pen pen = new Pen(BorderColor, BorderWidth))
                    {
                        g.DrawRectangle(pen, r);
                    }
                }
            }
        }

      

        private void btnMin_Click(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
                this.WindowState = FormWindowState.Minimized;
        }

        private void btnMax_Click(object sender, EventArgs e)
        {
            MaxWindow();
        }

        private void MaxWindow()
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
                btnMax.Text = "1";
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
                btnMax.Text = "2";
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void pTop_ControlRemoved(object sender, ControlEventArgs e)
        {
            switch (e.Control.Name)
            {
                case "btnMax":
                    {
                        if (pTop.Contains(btnMin))
                        {
                            btnMin.Left = btnClose.Left - 40;
                        }
                        break;
                    }

            }
        }

        private void pTop_DoubleClick(object sender, EventArgs e)
        {
            MaxWindow();
        }


    }
}
